# -*- coding: utf-8 -*-
# @Time   : 2023/9/13 16:20
# @Author : 大龙 🚀
# @File   : gansu_illegal.py
import re

from apps.tax_illegal.tax_illegal.items import NetTaxIllegalProItem
from apps.tax_illegal.tax_illegal.spiders import BaseTaxIllegalSpider
from utils.tools import urlencode


class XiamenIllegalSpider(BaseTaxIllegalSpider):
    name = "xiamen_illegal"
    province: str = "厦门"
    url: str = "http://xiamen.chinatax.gov.cn/xmswcms/wycx/zdss.html"  # 取首页

    def start_requests(self):
        yield from self.etax_search(**{"etax_url": "https://etax.xiamen.chinatax.gov.cn:8443"})
        url = "http://xiamen.chinatax.gov.cn:8004/front/dispatch/queryZdsswfsxajxx.do"
        data = {
            "pageNumber": "1",
            "pageSize": "10",
            "ssnd": "",
            "ssjd": "",
            "anjxz": "",
            "zgswj_dm": "",
            "yzbz": "N",
            "nsr_mc": "",
            "zcdz": "",
            "frdb": "",
            "cwfzr": "",
            "checkcode": "",
            "nsr_id": "",
            "zzjg_dm": "",
            "frzjhm": "",
            "cwrysfzhm": "",
            "b_query": "查询",
        }
        yield self.FormRequest(url, formdata=data, callback=self.parse_list)

    def parse_list(self, response, **kwargs):
        yield from self.parse(response, **kwargs)  # 第一页解析
        url = "http://xiamen.chinatax.gov.cn:8004/front/dispatch/queryZdsswfsxajxx.do"
        total = response.json()["pageCount"]
        if total:
            for page in range(2, int(total) + 1):
                data = {
                    "pageNumber": f"{page}",
                    "pageSize": "10",
                    "ssnd": "",
                    "ssjd": "",
                    "anjxz": "",
                    "zgswj_dm": "",
                    "yzbz": "N",
                    "nsr_mc": "",
                    "zcdz": "",
                    "frdb": "",
                    "cwfzr": "",
                    "checkcode": "",
                    "nsr_id": "",
                    "zzjg_dm": "",
                    "frzjhm": "",
                    "cwrysfzhm": "",
                    "b_query": "查询",
                }
                yield self.FormRequest(url, formdata=data, callback=self.parse)

    def parse(self, response, **kwargs):
        url = "http://xiamen.chinatax.gov.cn:8004/front/dispatch/sswfrightShow.do"
        row_list = response.json()["rows"]
        for row in row_list:
            params = {"wfid": row["wfid"]}
            yield self.Request(url + "?" + urlencode(params), callback=self.parse_detail, cb_kwargs={"row": row})

    def parse_detail(self, response, **kwargs):
        row = kwargs["row"]
        item = NetTaxIllegalProItem()
        item.province = self.province
        item.year = row["lrsj"][:10].replace("-", "")

        info = []
        for i in response.xpath('//table[@id="table_content"]//tr'):
            res = [i.xpath("string(.)").get().strip() for i in i.xpath(".//td")]
            if res:
                if len(res) != 2:
                    continue
                info.append(res)

        for k, *v in info:
            v = " ".join(v)
            k = re.sub(r"[^\u4e00-\u9fa5]+", "", k)
            if "纳税人名称" in k:
                v = re.sub(r"(<!--.*?-->|<.*?>|\s*)", "", v, flags=re.S)
                item.company_name = v
            if "纳税人识别号" in k:
                v = re.sub(r"(<!--.*?-->|<.*?>|\s*)", "", v, flags=re.S)
                item.taxpayer_id = v
            if "组织机构代码" in k:
                v = re.sub(r"(<!--.*?-->|<.*?>|\s*)", "", v, flags=re.S)
                item.org_code = v
            if "注册地址" in k:
                v = re.sub(r"(<!--.*?-->|<.*?>|\s*)", "", v, flags=re.S)
                item.city = item.resolve_register_address(v)[0]
                item.country = item.resolve_register_address(v)[1]
                item.address = v
            if "法定代表人或者负责人姓名性别及身份证号码或其他证件号码" in k:
                v = re.sub(r"(<!--.*?-->|<.*?>|\s*)", "", v, flags=re.S)
                item.legal_representative = v
            if "违法期间法定代表人或负责人姓名性别及身份证号码或其他证件号码" in k:
                v = re.sub(r"(<!--.*?-->|<.*?>|\s*)", "", v, flags=re.S)
                item.illegal_legal_representative = v
            if "负有直接责任的财务人员姓名性别及身份证号码或者其他证件号码" in k:
                v = " ".join(re.sub(r"(<!--.*?-->|<.*?>)", "", v, flags=re.S).split())
                item.resp_financial = v
            if "实际负责人姓名性别及身份证号码或者其他证件号码" in k:
                v = " ".join(re.sub(r"(<!--.*?-->|<.*?>)", "", v, flags=re.S).split())
                item.resp_person = v
            if "负有直接责任的中介机构信息" in k:
                v = " ".join(re.sub(r"(<!--.*?-->|<.*?>)", "", v, flags=re.S).split()).replace("null", "")
                item.resp_intermediary = v
            if "案件性质" in k:
                item.illegal_status = v
            if "主要违法事实" in k:
                v = re.sub(r"(<!--.*?-->|\s*|<.*?>)", "", v, flags=re.S)
                illegal_facts_src = re.sub(r"(<.*?>|\s*|“|”)", "", re.sub(r"(<!--.*?-->)", "", v))
                illegal_facts = re.findall(
                    r"(.*(?:主要存在|存在以下问题|发现其在)(?:[^根]*?[。，;；])*)[^。]*?(?:根据|依照|依据)+",
                    illegal_facts_src,
                )
                illegal_facts = illegal_facts[0] if illegal_facts else ""
                if illegal_facts:
                    basis_and_punishment = illegal_facts_src.replace(illegal_facts, "")
                else:
                    illegal_facts = illegal_facts_src
                    basis_and_punishment = ""
                item.basis_and_punishment = basis_and_punishment
                item.illegal_facts = illegal_facts

        yield item


if __name__ == "__main__":

    from scrapy import cmdline

    cmdline.execute(argv=["scrapy", "crawl", "xiamen_illegal"])
